package dao

import (
	"fmt"
	"go_gin_bubble/setting"

	"gorm.io/gorm"
	"gorm.io/driver/mysql"
)

//dao层是数据访问层，负责模型和数据库的交互
var DB *gorm.DB

func InitMySQL(conf *setting.MySQLConfig)(err error) {
	//示例 root:root123@tcp(127.0.0.1:306)/db?charset=utf8mb4
	//dsn data-source-name
	fmt.Println(conf.Host)
	// dsn := fmt.Sprintf("#{conf.User}:#{conf.Password}@tcp(#{conf.Host}:#{conf.Port})/#{conf.BD}?charset=utf8mb4&parseTime=True&loc=Local")
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		conf.UserName, conf.Password, conf.Host, conf.DBPort, conf.DB)
	//rot:root@tcp(%!s(int=0):3306)/go_bubble?charset=utf8mb4&parseTime=True&loc=Local
	fmt.Println(dsn)
	DB,err = gorm.Open(mysql.Open(dsn),&gorm.Config{})

	return 
}

func CloseDB(){
	
}